package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.exception.WebException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public Employee selectOne(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> getPageInfo(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> data = employeeMapper.getData(qo);
        return new PageInfo<>(data);
    }

    @Override
    public void insertEmployeeAndRole(Long eId, Long rId) {
        employeeMapper.insertEmployeeAndRole(eId,rId);
    }

    @Override
    public List<Role> selectRole(Long id) {
        return employeeMapper.selectRole(id);
    }

    @Override
    public void deleteEmployeeAndRole(Long id) {
        employeeMapper.deleteEmployeeAndRole(id);
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {
        //定义工作表
        Workbook wb = new HSSFWorkbook();
        //定义工作页
        Sheet sheet1 = wb.createSheet("员工通讯录");
        //定义行
        Row row = sheet1.createRow(0);
        //定义单元格信息
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工邮箱");
        List<Employee> employees = employeeMapper.selectAll();
        for (int i=0;i<employees.size();i++){
            Employee employee = employees.get(i);
            Row row1 = sheet1.createRow(i + 1);
            row1.createCell(0).setCellValue(employee.getId());
            row1.createCell(1).setCellValue(employee.getName());
            row1.createCell(2).setCellValue(employee.getEmail());
        }
        //将文件输出到浏览器中
        wb.write(outputStream);

    }

    @Override
    public  Long getId(String keyword) {
        return employeeMapper.getEmployeeId(keyword);
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //获取表格
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //获取表格的工作页
        Sheet sheetAt = workbook.getSheetAt(0);
        //获取最后一行
        int lastRowNum = sheetAt.getLastRowNum();
        //遍历总行数
        for (int i=1;i<=lastRowNum;i++){
            //获取每一行
            Row row1 = sheetAt.getRow(i);
            Employee employee = new Employee();
            employee.setName(row1.getCell(0).getStringCellValue());
            Double value = row1.getCell(1).getNumericCellValue();
            employee.setAge(value.intValue());
            employee.setEmail(row1.getCell(2).getStringCellValue());
            employee.setUsername("alex");
            employee.setAdmin(false);
            employee.setPassword("555");
            employeeMapper.insert(employee);
        }
    }

    @Override
    public List<Permission> getPermission(Long id) {
        List<Permission> permission = employeeMapper.getPermission(id);
        return permission;
    }

    @Override
    public Employee checkUser(String username,String password) {
        Employee employee = employeeMapper.checkUser(username);

        if (StringUtils.isEmpty(username)) {
            throw new WebException("账户不能为空!");
        }
        if (StringUtils.isEmpty(password)){
            throw new WebException("密码不能为空!");
        }
        if (employee == null || !employee.getPassword().equals(password)) {
            throw new WebException("账户或密码错误!");
        }
        return employee;
    }

}
